package com.endbd.systemmanagement.controller.module;

import com.endbd.feignapi.model.system.Module;
import com.endbd.systemmanagement.model.ResultBody;
import com.endbd.systemmanagement.service.module.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/system")
@RestController
public class ModuleController {

  @Autowired
  private ModuleService moduleService;

  @PostMapping("/module/moduleList")
  public ResultBody getModuleList(@RequestBody Module module){
    List<Module> list=moduleService.queryModule(module);
    ResultBody resultBody = new ResultBody();
    if(list.isEmpty()){
      resultBody.setCode(500);
      resultBody.setMessage("模块数据查询失败");
    }else{
      resultBody.setCode(200);
      resultBody.setMessage("success");
      resultBody.setData(list);
    }
    return resultBody;
  }

  @GetMapping("/module/getParent")
  public ResultBody queryParent(){
    List<Module> list=moduleService.queryParent();
    ResultBody resultBody = new ResultBody();
    if(list.isEmpty()){
      resultBody.setCode(500);
      resultBody.setMessage("模块数据查询失败");
    }else{
      resultBody.setCode(200);
      resultBody.setMessage("success");
      resultBody.setData(list);
    }
    return resultBody;
  }

  @PostMapping("/module/insert")
  public ResultBody insertModule(@RequestBody Module module){
    boolean b=moduleService.insertModule(module);
    ResultBody resultBody = new ResultBody();
    if(b){
      resultBody.setCode(200);
      resultBody.setMessage("添加新的模块==>"+module.getModuleRName());
    }else{
      resultBody.setCode(500);
      resultBody.setMessage("error");
    }
    return resultBody;
  }

  @GetMapping("/module/disabled/{id}/{state}")
  public ResultBody disabled(@PathVariable Integer id, @PathVariable String state){
    boolean b=moduleService.disabled(id,state);
    ResultBody resultBody = new ResultBody();
    if(b){
      resultBody.setCode(200);
      resultBody.setMessage("更改模块状态==>"+id+"==>"+(state.equals("1") ? "注销" : "开启"));
    }else{
      resultBody.setCode(500);
      resultBody.setMessage("操作失败");
    }
    return resultBody;
  }

  @PostMapping("/module/update")
  public ResultBody update(@RequestBody Module module){
    boolean b=moduleService.updateModule(module);
    ResultBody resultBody = new ResultBody();
    if(b){
      resultBody.setCode(200);
      resultBody.setMessage("更改模块==>"+module.getModuleRName());
    }else{
      resultBody.setCode(500);
      resultBody.setMessage("修改失败");
    }
    return resultBody;
  }

}
